package ru.ifmo.itmokis;

import ru.ifmo.itmokis.sim.ProductionQueue;
import java.util.Iterator;
import ru.ifmo.itmokis.util.DataContext;
import ru.ifmo.itmokis.algo.Optimizer;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import ru.ifmo.itmokis.domain.Material;
import ru.ifmo.itmokis.domain.Order;
import ru.ifmo.itmokis.domain.OrderedProduct;
import ru.ifmo.itmokis.domain.Supply;

/**
 *
 * @author Ivan Stepuk
 */
public class Facade {

    private ProductionQueue productionQueue;
    private Optimizer optimizer;
    private DataContext dataContext;

    public Facade(ProductionQueue productionQueue, DataContext dataContext, Optimizer optimizer) {
        this.productionQueue = productionQueue;
        this.dataContext = dataContext;
        this.optimizer = optimizer;
    }

    public void registerNewOrder(Order order, List<OrderedProduct> products) {
        dataContext.persist(order);
        for (OrderedProduct op : products) {
            productionQueue.insertProduct(op);
        }
        optimizer.optimize();
        productionQueue.setDeadlines();
    }

    public void forceOptimization() {
        optimizer.optimize();
    }

    @Deprecated
    public void orderMaterialSupply(Supply supply) {
        dataContext.persist(supply);
    }

    public void deliverNewMaterials(Supply supply) {
//        Iterator<Entry<Material, Integer>> it = materials.entrySet().iterator();
//        while (it.hasNext()) {
//            Entry<Material, Integer> entry = it.next();
//            dataContext.updateMaterialQty(entry.getKey(), entry.getValue());
//        }
        dataContext.updateMaterialQty(supply.getMaterial(), supply.getQty());
    }
}
